﻿using System;
using System.Collections.Generic;
using BlockLogic.Interface;
using BlockLogic.Interface.Blocks;
using BlockLogic.Interface.Controller;
using BlockLogic.Interface.Blocks.Checker;

namespace BlockLogic.Impl.Controller
{
    public class BlockController : IBlockController
    {
        private static BlockController _instance = new BlockController();

        private List<IBlock> _blocks;

        private BlockController()
        {
            _blocks = new List<IBlock>();
        }

        public static IBlockController GetInstance()
        {
            return _instance;
        }

        public List<IBlock> Blocks
        {
            get { return _blocks; }
            set {}
        }

        public IBlock this [Guid id]
        {
            get { return _blocks.Find(x => x.ID == id);}
        }

        public BlockController Instance()
        {
            return _instance;
        }

        public void AddBlock(IBlock block)
        {
            _blocks.Add(block);
        }

        public void RemoveBlock(Guid id)
        {
            _blocks.Remove(_blocks.Find(x => x.ID == id));
        }

        public void Connect(IInputBlock inBlock, IOutputBlock outBlock)
        {
            if (inBlock is ICheckerBlock)
            {
                outBlock.CheckerBuffer = inBlock.InputBuffer;
            }
            else
            {
                outBlock.OutputBuffer = inBlock.InputBuffer;
            }
        }

        public void Disconnect(IInputBlock inBlock, IOutputBlock outBlock)
        {
            if (inBlock is ICheckerBlock)
            {
                outBlock.CheckerBuffer = null;
            }
            else
            {
                outBlock.OutputBuffer = null;
            }
        }
    }
}
